﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;


using WatiN.Core;
using WatiN.Core.Native;
using WatiN.Core.Logging;
using WatiN.Core.UtilityClasses;
using WatiN.Core.Properties;
using WatiN.Core.Native.Windows;
using WatiN.Core.DialogHandlers;

using WatiNSahi.Extensions;

namespace WatiNSahi
{
    public abstract partial class WatiNSahiTest
    {
        #region PRIVATE STATIC VARIABLES

        private static Browser browser { get; set; }//, br1=null, br2=null, br3=null, b4 = null;
        private static IntPtr hwnd;
        private static int PID;

        #endregion    
        
        #region PUBLIC STATIC VARIABLES

        /// <summary>
        /// Set the browser type to replay the test case.
        /// Declare the browserType before calling _navigateTo(url); .
        /// Takes string with supported values as "firefox" or "ie".
        /// Value for Chrome Browser "chrome" is not implemented yet.
        /// </summary>
        /// <example>browserType="firefox"</example>
        public static string browserName = "ie";

        public static string baseURL = null;



        #endregion

        private static void _initialize()
        {
            if ("firefox".Equals(browserName))
            {
                Console.WriteLine("Browser Name is \"firefox\". Using Firefox browser for replay.");
                browser = new FireFox();
                _wait(1500);
            }
            else
            {
                Console.WriteLine("Browser Name is \"ie\" or not specified. Using IE as default browser.");
                Console.WriteLine("Browser Name can be set to \"firefox\" by declaring the browserName=\"firefox\";");

                WatiN.Core.Settings.MakeNewIe8InstanceNoMerge = true;

                browser = new IE(WatiNSahi.Extensions.ExtensionsSettings.CreateIEInNewProcess);

            }
        }


        public static Browser _browser()
        {
            return browser;
        }

        public static void _alert(string message)
        {
            browser.RunScript(string.Format("alert('{0}')",message));
        }
        public static void _callServer()
        {

        }
        public static void _click()
        {

        }
        public static void _clickLinkByAccessor()
        {

        }
        public static void _dragDrop()
        {

        }
        public static void _getSelectedText()
        {

        }
        public static void _resetSavedRandom()
        {

        }
        public static void _setSelected()
        {

        }
        


        public static void _simulateEvent()
        {

        }
        public static void _call()
        {

        }
        public static void _eval()
        {

        }
        public static void _setGlobal()
        {

        }


        #region WAIT FUNCTIONS


        /// <summary>
        /// Wait for given milli seconds
        /// </summary>
        /// <param name="milliSeconds"></param>
        public static void _wait(int milliSeconds)
        {

            Thread.Sleep(milliSeconds);
        }

        /// <summary>
        /// Explicit wait for complete.
        /// This will wait for 90 sec timeout.
        /// </summary>
        public static void _wait_for_complete()
        {
            browser.WaitForComplete(90);
        }

        public static void _wait_for_complete(int timeoutInSeconds)
        {
            browser.WaitForComplete(timeoutInSeconds);
        }


        #endregion

        public static void _highlight()
        {

        }

        private static void _log(string type, string message)
        {

            string logType = "MESSAGE LOG";

            if("error".Equals(type))
            {
                logType = "EXCEPTION LOG";
            }
            else if("fail".Equals(type))
            {
                logType = "FAILURE LOG";
            }
            else if ("".Equals(type))
            {
                logType = "MESSAGE LOG";
            }
            else
            {
                logType = type;
            }



            string dateStr = null;
            dateStr = DateTime.Now.ToString();
            Console.WriteLine(string.Format("'{0}'\t\t ['{1}'] :: '{2}'",
                logType, dateStr, message));

        }

        public static void _log(string message)
        {
            _log("", message);
        }

        public static void _logException(string message)
        {
            _log("error", message);
        }
        public static void _logExceptionAsFailure(string message)
        {
            _log("fail", message);
        }

        public static void _doubleClick()
        {

        }
        public static void _rightClick()
        {

        }
        public static void _addMock()
        {

        }
        public static void _removeMock()
        {

        }
        public static void _expectConfirm()
        {

        }
        public static void _setFile()
        {

        }
        public static void _expectPrompt()
        {

        }
        public static void _debug()
        {

        }
        public static void _debugToErr()
        {

        }
        public static void _debugToFile()
        {

        }
        
        public static void _mockImage()
        {

        }
        public static void _execute()
        {

        }

        public static void _enableKeepAlive()
        {

        }
        public static void _disableKeepAlive()
        {

        }
        public static void _dragDropXY()
        {

        }
        public static void _deleteCookie()
        {

        }
        public static void _createCookie()
        {

        }
        public static void _clearPrintCalled()
        {

        }
        public static void _saveDownloadedAs()
        {

        }
        public static void _clearLastDownloadedFileName()
        {

        }
        public static void _clearLastAlert()
        {

        }
        public static void _clearLastPrompt()
        {

        }
        public static void _clearLastConfirm()
        {

        }
        public static void _closeWindow()
        {

        }
        public static void _collect()
        {

        }
        public static void _count()
        {

        }
        public static void _event()
        {

        }
        public static void _getGlobal()
        {

        }

        public static Browser _window(string sTitle)
        {
            Browser newWindow = Browser.AttachTo(browser.GetType(), Find.ByTitle(sTitle));

            return newWindow;
        }


        public static bool _contains(Element parentElm, Element childElm)
        {
            bool hasGivenChild = false;

            ElementCollection children = parentElm.DomContainer.Elements;
            foreach (var child in children)
            {
                if (childElm.Equals(child))
                    hasGivenChild = true;
            }

            return hasGivenChild;
        }
        public static bool _containsText(string sText)
        {
            if (browser.ContainsText(sText))
                return true;
            else
                return false;
        }
        public static bool _containsHTML()
        {
            return true;
        }



        public static void _lastConfirm()
        {

        }


        public static bool _exists(Element elm)
        {
            if (elm.Exists)
                return true;
            else
                return false;
        }






        public static void _getDB()
        {

        }
        public static void _readFile()
        {

        }
        public static void _readURL()
        {

        }
        public static void _lastPrompt()
        {

        }
        public static void _lastAlert()
        {

        }
        public static void _get()
        {

        }
        public static void _style()
        {

        }
        
        public static void _cookie()
        {

        }
        public static void _position()
        {

        }
        public static void _print()
        {

        }
        public static void _printCalled()
        {

        }

        public static void _lastDownloadedFileName()
        {

        }
        public static void _rteHTML()
        {

        }
        public static void _rteText()
        {

        }
        public static void _re()
        {

        }
        public static void _prompt()
        {

        }
        public static void _scriptName()
        {

        }

        public static bool _isVisible(Element elm)
        {
            if(IsDisplayed(elm))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        

        private static bool IsDisplayed(Element element)
        {
            if (string.Equals(element.Style.Display, "none") || (element.GetAttributeValue("type") == "hidden"))
            {                
                return false;
            }
            if (element.Parent != null)
            {
                return IsDisplayed(element.Parent);
            }
            return true;
        }

        public static void _stopOnError()
        {

        }
        public static void _continueOnError()
        {

        }
        
        
        public static void _scriptPath()
        {

        }


        /**
        * 
        * @return window
        */
        public static Browser _popup(string title)
        {
            Browser window;
            window = Browser.AttachTo(browser.GetType(), Find.ByTitle(title));

            return window;
        }
        public static void _include()
        {

        }
        public static bool _condition()
        {

            return true;
        }
        public static void _sahi()
        {

        }
        public static void _set()
        {

        }

    }
}
